package Demo;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class UserSystem {
    public static void  main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //定义三只系统自带的宠物用于赠送给注册的用户
        Pets p1 = new Pets("蓝精灵",200,20,50);
        Pets p2 = new Pets("阿木木",200,15,60);
        Pets p3 = new Pets("火花",200,25,40);
        //将这三只宠物加入宠物管理场
        list0.add(p1);
        list0.add(p2);
        list0.add(p3);
        while (true) {
            System.out.println("欢迎来到宠物对战模拟器界面:");
            System.out.println("1. 去用户界面");
            System.out.println("2. 去管理员界面");
            System.out.println("3. 退出");
            System.out.print("请输入你的选择: ");
            String choice = scanner.next();
            switch (choice) {
                case "1":
                    main1();
                    break;
                case "2":
                    main2();
                    break;
                case "3":
                    System.exit(0);
                default:
                    System.out.println("当前选项不存在，请重新输入");
            }
        }
    }
    public static void main1() {
        Scanner scanner = new Scanner(System.in);
// 用户菜单
        temp:while (true) {
            System.out.println("欢迎来到用户界面:");
            System.out.println("1. 注册用户");
            System.out.println("2. 用户登录");
            System.out.println("3. 查看宠物背包");
            System.out.println("4. 修改用户个人信息");
            System.out.println("5. 退出");
            System.out.print("请输入你的选择: ");
            int choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    addUser(list,list1);
                    break;
                case 2:
                    loginUser(list);
                    break;
                case 3:
                    viewPetBag(list1);
                    break;
                case 4:
                    updatePersonalInfo(list);
                    break;
                case 5:
                    break temp;
                default:
                    System.out.println("当前选项不存在，请重新输入");
            }
        }


    }
    //修改用户个人信息
    private static void updatePersonalInfo(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要修改的用户名：");
        String username = sc.next();
        int index = getIndex(list,username);
        if(index==-1){
            System.out.println("当前用户名不存在，请重新输入");
            return;
        }
        User u = list.get(index);
        System.out.println("请输入你要修改用户名为：");
        String newusername= sc.next();
        u.setUsername(newusername);
        System.out.println("请输入你要修改用户昵称为：");
        String newnickname = sc.next();
        u.setNickname(newnickname);
        System.out.println("请输入你要修改用户个性签名为：");
        String newsignname = sc.next();
        u.setSignname(newsignname);
        System.out.println("请输入你要修改用户的出生日期为：");
        String newbirthday = sc.next();
        u.setBirthday(newbirthday);
    }
    //查询宠物背包宠物信息
    private static void viewPetBag(ArrayList<Pets>list1) {
            for (int i = 0; i < list1.size(); i++) {
            System.out.println("宠物信息：");
            System.out.println("宠物姓名为："+list1.get(i).getName());
            System.out.println("宠物血量为："+list1.get(i).getHealth());
            System.out.println("宠物攻击力为"+list1.get(i).getAttack());
            System.out.println("宠物防御力为："+list1.get(i).getDefense());
        }

    }
    //用户登录
    private static void loginUser(ArrayList<User> list){
        for (int i = 0; i < 3; i++) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入用户名：");
            String username = sc.next();
            boolean flag = contain(list, username);
            if (!flag) {
                System.out.println("用户名" + username + "未注册，请先注册再登陆");
                return;
            }
            System.out.println("请输入密码");
            String password = sc.next();

            while (true) {
                String rightcode = getCode();
                System.out.println("当前正确的验证码为：" + rightcode);
                System.out.println("请输入验证码：");
                String code = sc.next();
                if (code.equalsIgnoreCase(rightcode)) {
                    System.out.println("验证码正确");
                    break;
                } else {
                    System.out.println("验证码错误，请重新输入");
                    continue;
                }
            }
            User userInfo = new User(username, password);
            boolean result = checkInfo(list, userInfo);
            if (result) {
                System.out.println("登录成功，你可以开始你的宠物对战之旅啦");
                break;
            } else {
                if(i==2){
                    System.out.println("用户"+username+"已经被管理系统锁定，请联系管理人员解所");
                }else{
                    System.out.println("当前用户名或者密码错误，还剩下"+(2-i)+"次机会");
                }

            }
        }

    }
//获取验证码
    private static String getCode() {
        ArrayList<Character> list = new ArrayList<>();
        for (int i = 0; i < 26; i++) {
            list.add((char)('a'+i));
            list.add((char)('A'+i));
        }

        StringBuilder sb = new StringBuilder();
        Random r = new Random();
        for (int i = 0; i < 4; i++) {
            int randomindex = r.nextInt(list.size());
            char c = list.get(randomindex);
            sb.append(c);
        }
        int number = r.nextInt(10);
        sb.append(number);
        char[] arr = sb.toString().toCharArray();
        int randomnumber = r.nextInt(arr.length);
        char temp = arr[randomnumber];
        arr[randomnumber] = arr[arr.length-1];
        arr[arr.length-1]=temp;
        return new String(arr);
    }
    //用户注册
    public static void addUser(ArrayList<User>list,ArrayList<Pets>list1){
        //定义三只系统自带的宠物用于赠送给注册的用户
        Pets p1 = new Pets("蓝精灵",200,20,50);
        Pets p2 = new Pets("阿木木",200,15,60);
        Pets p3 = new Pets("火花",200,25,40);
        User u = new User();
        while (true) {
            Scanner sc = new Scanner(System.in);
            String username;
            while (true) {
                System.out.println("请输入要注册的用户名");
                username = sc.next();
                boolean flag = contain(list, username);
                if (flag) {
                    System.out.println("当前用户名已经存在，请重新输入");
                } else {
                    u.setUsername(username);
                    break;
                }
            }
            while (true) {
                System.out.println("请输入您的密码");
                String password1 = sc.next();
                System.out.println("请再次输入的你密码");
                String password2 = sc.next();
                if (password1.equals(password2)) {
                    System.out.println("密码" + "******" + "设置成功");
                    u.setPassword(password1);
                    break;
                } else {
                    System.out.println("两次密码输入不一致，请重新输入");
                }
            }
            //将注册的用户添加到用户集合中
            System.out.println("注册成功，请在下列系统自带宠物中挑一只宠物添加到宠物背包");
            System.out.println("水属性宠物：(输入1)");
            System.out.println("宠物名称为：" + p1.getName() + "血量：" + p1.getHealth()
                    + "攻击力为：" + p1.getAttack() + "防御力为：" + p1.getDefense());
            System.out.println("木属性宠物：（输入2）");
            System.out.println("宠物名称为：" + p2.getName() + "血量：" + p2.getHealth()
                    + "攻击力为：" + p2.getAttack() + "防御力为：" + p1.getDefense());
            System.out.println("火属性宠物：（输入3）");
            System.out.println("宠物名称为：" + p3.getName() + "血量：" + p3.getHealth()
                    + "攻击力为：" + p3.getAttack() + "防御力为：" + p3.getDefense());
            while (true) {
                int number = sc.nextInt();
                System.out.println("请输入你的选项");
                if (number == 1) {
                    list1.add(p1);
                    System.out.println("水精灵已经添加进你的宠物背包");
                    break;
                } else if (number == 2) {
                    list1.add(p2);
                    System.out.println("阿木木已经添加进你的宠物背包");
                    break;
                } else if (number == 3) {
                    list1.add(p3);
                    System.out.println("火花已经添加进你的宠物背包");
                    break;
                }else {System.out.println("选项不存在，请重新输入");}

            }break;
        }
    }
    //用于判断用户登陆时用户名和密码是否都输入正确
    public static boolean checkInfo(ArrayList<User> list, User userInfo){
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            if((user.getUsername().equals(userInfo.getUsername()))&&(user.getPassword().equals(userInfo.getPassword()))){
                return true;
            }
        }
        return false;
    }
    //用于判断用户名是否已经存在
    public static boolean contain(ArrayList<User>list, String username){
        return getIndex(list,username)>=0;
    }
    public static int getIndex(ArrayList<User>list,String username){
        for (int i = 0; i < list.size(); i++) {
            User u = list.get(i);
            String name = u.getUsername();
            if(name.equals(username)){
                return i;
            }
        }
        return -1;
    }
    //管理员系统
    public static void main2() {
            //管理员菜单
            Scanner sc = new Scanner(System.in);
                opp:while (true) {
                    System.out.println("欢迎来到管理员操作界面：");
                    System.out.println("1.管理宠物场");
                    System.out.println("2.设立游戏规则");
                    System.out.println("3.查看用户信息");
                    System.out.println("4.退出");
                    System.out.print("请输入你的选择: ");
                    String choice = sc.next();
                    User u = new User();
                    switch (choice) {
                        case "1":
                            maintainPetGrowField(sc);
                            break;
                        case "2":
                            setGameRules(sc);
                            break;
                        case "3":
                            viewUserInfoAndPetBag(list);
                            break;
                        case "4":
                            break opp;
                        default:
                            System.out.println("当前选项不存在，请重新输入");
                    }
                }
            }
            //设立游戏规则
        private static void setGameRules(Scanner sc) {
        Random r = new Random();
        int index1 = r.nextInt(list0.size());
        int index2 = r.nextInt(list0.size());
         Pets p1 = list0.get(index1);
         Pets p2 = list0.get(index2);
         p1.showPetInfo();
         p2.showPetInfo();
            System.out.println("Pk:");
            while(true){
                p1.attack(p2);
                if(p1.getHealth()==0){
                    System.out.println(p1.getName()+"K.O"+p2.getName());
                    break;
                }
                p2.attack(p1);
                if(p1.getHealth()==0){
                    System.out.println(p2.getName()+"K.O"+p1.getName());
                    break;
                }
            }
        }
        private static ArrayList<Pets>list0 = new ArrayList<>();
        public static ArrayList<User>list= new ArrayList<>();
        public static ArrayList<Pets>list1=new ArrayList<>();

        // 管理宠物场
        public static void maintainPetGrowField(Scanner sc){
            tip:while(true) {
                System.out.println("欢迎管理员来到宠物场，请输入你的选项：");
                System.out.println("1. 添加宠物");
                System.out.println("2. 删除宠物");
                System.out.println("3. 观察宠物列表");
                System.out.println("4. 退出");
                System.out.print("请输入你的选择: ");
                String choice = sc.next();
                switch (choice) {
                    case "1":
                        addPettoPetGrowField(sc);
                        break;
                    case "2":
                        removePetFromGrowthField(sc);
                        break;
                    case "3":
                        viewAllpets(list0);
                        break;
                    case "4":
                        break tip;
                        default:
                        System.out.println("当前选项不存在，请重新输入选项：");
                }
            }
        }
        //管理员添加宠物进宠物管理场
        public static void addPettoPetGrowField(Scanner sc){
            System.out.print("请输入宠物姓名: ");
            String name = sc.next();
            System.out.print("请输入宠物血量: ");
            int health = sc.nextInt();
            System.out.print("请输入宠物攻击力: ");
            int attack = sc.nextInt();
            System.out.print("请输入宠物防御力: ");
            int defense = sc.nextInt();
            Pets pet = new Pets(name, health, attack, defense);
            list0.add(pet);
            System.out.println("宠物添加成功");
        }
        //管理员删除宠物
        public static void removePetFromGrowthField(Scanner sc){
            System.out.print("输入宠物的姓名用于删除宠物: ");
            String name = sc.next();
            list0.removeIf(pets -> pets.getName().equals(name));//如果姓名相同就删除
        }
        //管理员查看宠物场所有信息
        public static void viewAllpets(ArrayList<Pets>list0){
        for (int i = 0; i < list0.size(); i++) {
                System.out.println("宠物信息：");
                System.out.println("宠物姓名为："+list0.get(i).getName());
                System.out.println("宠物血量为："+list0.get(i).getHealth());
                System.out.println("宠物攻击力为"+list0.get(i).getAttack());
                System.out.println("宠物防御力为："+list0.get(i).getDefense());
            }
        }
        //管理员查看用户信息及用户宠物背包
        public static void viewUserInfoAndPetBag(ArrayList<User>list){
            System.out.println("请输入你要查询的用户名");
            for (int i = 0; i < list.size(); i++) {
                System.out.println("用户信息：");
                System.out.println("用户名名为："+list.get(i).getUsername());
                System.out.println("用户昵称为："+list.get(i).getNickname());
                System.out.println("用户个性签名为"+list.get(i).getSignname());
                System.out.println("用户生日为："+list.get(i).getBirthday());
                System.out.println("用户宠物背包为："+list.get(i).getPets());
            }
        }
}

